#include "dialogs.h"
#include "../resource/resource.h"

#include <windows.h>

StatusWindow::StatusWindow()
{
}

StatusWindow::~StatusWindow()
{
}

HWND StatusWindow::GetWindow() const
{
    return m_hWnd;
}

void StatusWindow::Update(const std::string &str)
{
    g_statusText = str;

    m_bStatusTextChanged = true;
}

void StatusWindow::Initialize()
{
    m_bStatusTextChanged = false;

    m_hWnd = CreateDialog(
                 GetModuleHandle(0), //    HINSTANCE hInstance,
                 MAKEINTRESOURCE(IDD_STATUS), //    LPCTSTR lpTemplate,
                 0, //    HWND hWndParent,
                 0 //    DLGPROC lpDialogFunc
             );
}

void StatusWindow::Run()
{
    Initialize();

    while (!m_finished && IsWindow(m_hWnd))
    {
        if (m_bStatusTextChanged)
        {
            SendDlgItemMessage(m_hWnd, IDC_STATUS, WM_SETTEXT, 0, (LPARAM)g_statusText.c_str());
            m_bStatusTextChanged = false;
        }

        MSG msg;
        while (PeekMessage(&msg, m_hWnd, 0L, 0L, PM_REMOVE))
        {
            // if the X was clicked
            if (msg.message == WM_COMMAND)
            {
                DestroyWindow(m_hWnd);
                return;
            }

            // process the message
            if (!IsDialogMessage(m_hWnd, &msg))
            {
                TranslateMessage(&msg);
                DispatchMessage(&msg);
            }
        }

        Sleep(1);
    }

    DestroyWindow(m_hWnd);
}
